package com.liwq.config;

import com.alibaba.dubbo.config.*;
import com.liwq.service.DefaultApiService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;




@Configuration
public class MyDubboConfig {

    //<dubbo:application name="boot-user-service-provider"></dubbo:application>
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("user-service-sonsumer-2");
        return applicationConfig;
    }

    //<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        // redis 注册中心
        registryConfig.setAddress("redis://localhost:6379");
        registryConfig.setProtocol("dubbo");
        registryConfig.setTimeout(5000);

        /*//zookeeper 注册中心
        registryConfig.setAddress("127.0.0.1");
        registryConfig.setPort(2181);
        registryConfig.setProtocol("zookeeper");
        registryConfig.setCheck(false);*/
        return registryConfig;
    }

    //<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20883);
        return protocolConfig;
    }


    /**
     *<dubbo:service interface="com.zang.gmall.service.UserService"
     ref="userServiceImpl01" timeout="1000" version="1.0.0">
     <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
     </dubbo:service>
     */
    @Bean
    public ReferenceConfig<DefaultApiService> userReferenceConfig() {
        ReferenceConfig<DefaultApiService> serviceConfig = new ReferenceConfig<>();
        serviceConfig.setInterface(DefaultApiService.class);
        serviceConfig.setTimeout(5000);
        serviceConfig.setVersion("*");
        serviceConfig.setRetries(3);


        //配置每一个method的信息
        MethodConfig methodConfig = new MethodConfig();
        methodConfig.setName("defaultMethod");
        methodConfig.setTimeout(1000);

        //将method的设置关联到service配置中
        List<MethodConfig> methods = new ArrayList<>();
        methods.add(methodConfig);
        serviceConfig.setMethods(methods);

        return serviceConfig;
    }

    //	<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>
    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }


    //	<dubbo:monitor address="127.0.0.1:7010"></dubbo:monitor>
    @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig monitorConfig = new MonitorConfig();
        //monitorConfig.setAddress("127.0.0.1:7010");
        monitorConfig.setProtocol("registry");
        return monitorConfig;
    }

}